var request = require('request');
var http = require('http');
//var iconv = require('iconv-lite');

function diyi(wangzhi) {
  return new Promise(function (resolve,reject) {

    //fs.readFile(path, 'utf8', function (err, data) 
    request(wangzhi, function (err, response,body)
    {
      if(err){
        reject(err);   //失败
      }else{
        var regExp=/document.getElementById\("Frm_Logintoken"\).value = "(.*)";/;
    var regExp1=/document.getElementById\("Frm_Loginchecktoken"\).value = (\d+);/;
    var res = regExp.exec(body);
    var res1 = regExp1.exec(body);
    var hh="Frm_Logintoken=" +res[1] + "&Frm_Loginchecktoken=" + res1[1] + "&_cu_url=0&Right=2&Username=&UserRandomNum=95670920&Password=0b1c0803d1299bcc3f6d4bd997adef8c76a2f234f21fb9cb812d884f1568e88f&action=login";
  





        resolve(hh);    //成功




        
      }
    })
  });
}


function dier(wangzhi,diyif) {
  return new Promise(function (resolve,reject) {

    //fs.readFile(path, 'utf8', function (err, data) 
    request({url:wangzhi,method:"POST",headers:{'Cookie': '_TESTCOOKIESUPPORT=1'},body:diyif}, function (err, response,body)
    {
      if(err){
        reject(err);   //失败
      }else{
        
        var sid=response.headers['set-cookie'][0].split(';')[0];
        resolve(sid);    //成功
      }
    })
  });
}


function disan(wangzhi,sid){
  return new Promise(function (resolve,reject) {

    //fs.readFile(path, 'utf8', function (err, data) 
    request({url:wangzhi,method:"GET",headers:{'Cookie': sid}}, function (err, response,body)
    {
      if(err){
        reject(err);   //失败
      }else{


    var regExp3=/Transfer_meaning\('SrcMacAddr\d+','(.*)'\);/g;
        var regExp6=/\\x\d[a-z]/g;
        var mingdan='"';
        while(res=regExp3.exec(body)){
            var uu=res[1].replace(regExp6,':');

            //console.log(uu);
            //mingdan.push(uu);
           mingdan=mingdan+uu+'","';

        }
        var gg='{"mingdan":['+mingdan.slice(0,-2)+']}';
        //console.log(gg);
        resolve(gg);




        //resolve(body);    //成功
      }
    })
  });
}

async function Test(response){
      //console.log(bb+1);
  let diyif=await diyi('http://192.168.1.1');
  let sid=await dier('http://192.168.1.1',diyif)
  let xy=await disan('http://192.168.1.1/getpage.gch?pid=1002&nextpage=user_sec_macfilter_conf_t.gch',sid)


   //var ty="89:'";
   //var ty='{"mingdan":["dc:f0:90:b0:3f:88"]}';
   //response.end(ty);
  response.end(xy);
  //response.end('{"mingdan":["dc:f0:90:b0:3f:88"]}');
  console.log(xy);

  //console.log("55");


}

http.createServer(function (request, response) {

    // 发送 HTTP 头部 
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200, {'Content-Type': 'text/plain'});//这个屏蔽的话
    Test(response);

    // 发送响应数据 "Hello World"
    //response.end('Hello World\n');
}).listen(8080);

// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');











//console.log(shuchu);